#!/usr/bin/env python
from lib.format_time import *
from lib.HandleMysql import *
from lib.handle_mongo import *
from lib.switch_code import *
from lib.load_data import *
from zhineng_conn import *
from create_table import *
from config_tools import *
import urllib
import sys
import re
import time
reload(sys)
sys.setdefaultencoding('utf-8')


def check_click_info(date):


    date_str = date.replace('-',"")
    line_list = []
    tomorrow = var_date(date).replace('-', "")
    print tomorrow
    total_income = 0.0
    error_split_count = 0
    for i in range(24):
        j = '%s' % i
        if i < 10:
            j = "0%s" % i

        url_str = "http://10.5.12.160:50075/streamFile/dsap/rawdata/znlog/%s/clicks_%s%s" % (date_str, date_str, j)
        if i > 21:
            url_str = "http://10.5.12.160:50075/streamFile/dsap/rawdata/znlog/%s/clicks_%s%s" % (tomorrow, date_str, j)

        print url_str

        file_path = "/work3/opt/analytic/lh_tmp_data/zhineng/check_file/click_data/check_click_info_%s_%s.txt" % (date_str, i)
        print "file_path = %s" % file_path
        #print url_str,file_path
        urllib.urlretrieve(url_str, file_path)
        f = open(file_path)
        stat_conn = set_stats_conn()
        dict_conn = set_dict_conn()
        for line in f.readlines():
            arr = line.split('\001')
            if len(arr) < 37:
                error_split_count = error_split_count + 1
                continue

            click_id = arr[13]
            click_time_id = arr[5]
            click_time = format_timestamp(click_time_id)
            user_id = arr[14]
            plan_id = arr[15]
            unit_id = arr[16]
            entity_id = arr[17]
            entity_type = arr[18]
            entity_city_id = arr[19]
            entity_cate_id = arr[20]
            display_id = arr[21]
            city = arr[7]
            cate = arr[6]
            display_position = arr[23]
            sort_position = arr[24]
            amount_str = arr[25]
            state_code = arr[26]
            state_val = arr[27]
            click_state = 0
            amount = 0.0
            bid = 0
            if state_code == "1111111" or state_code == "21" or state_code == "22":
                click_state = 1
            if (amount_str != None) and (amount_str != ''):
                amount = float(amount_str)/100
            budget_amount = arr[31]
            bid_str = arr[33]
            if bid_str == '-':
                bid_str = '0'
            ## income
            try:
                bid_str = bid_str.replace('\n','')
                bid = float(bid_str)/100
            except Exception:
                pass
            if click_state == 1:
                total_income += amount
            cash_amount = 0.0
            discount_amount = 0.0
            if click_state == 1:
                q = "select cash_amount,discount_amount from ad_order_info where click_id='%s'" % click_id
                print "select order: %s." % q
                try:
                    items =stat_conn.get_results(q)
                    if len(items) > 0 :
                        cash_amount = items[0][0]
                        discount_amount = items[0][1]
                except Exception,e:
                    print 'create table error:',e


            dc = get_cates(cate,dict_conn)
            cat1 = 0
            cat1name = ''
            cat2 = 0
            cat2name=''
            cat3 = 0
            cat3name=''
            if dc != None:
                if len(dc) > 0:
                    cat1 = dc[0][0]
                    cat1name = dc[0][1]
                if len(dc) > 1:
                    cat2 = dc[1][0]
                    cat2name=dc[1][1]
                if len(dc) > 2:
                    cat3 = dc[2][0]
                    cat3name=dc[2][1]

            city_id = 0
            city_name = ''
            area_id = 0
            area_name = ''
            local_id= 0
            local_name = ''
            if city != '0':
                dc = get_citys(city,dict_conn)
                if dc != None:
                    if len(dc) >0:
                        city_id = dc[0][0]
                        city_name = dc[0][1]
                    if len(dc)>1:
                        area_id = dc[1][0]
                        area_name = dc[1][1]
                    if len(dc)>2:
                        local_id= dc[2][0]
                        local_name = dc[2][1]

            param = [date, click_id, click_time, user_id, plan_id, unit_id, entity_id, entity_type,entity_cate_id,entity_city_id,city_id,city_name,area_id,area_name,local_id,
                     local_name,cat1,cat1name,cat2,cat2name,cat3,cat3name,display_id,display_position,sort_position,
                     amount,cash_amount,discount_amount,budget_amount,bid,state_code,state_val,click_state]
            #print param
            s_line = '$$'.join(map(lambda i: str(i), param))
            s_line = s_line.replace('\t','')
            line_list.append(s_line)
        stat_conn.close()
        dict_conn.close()

        cont = '\n'.join(line_list)
        path = "/work3/opt/analytic/lh_tmp_data/zhineng/check_file/click_data/"
        file_name = "click_data_%s_%s.txt" % (date_str, i)
        writeTextToFile(path, file_name, "w", cont)



    print "total income equal %s. and error split count equal %s" % (total_income, error_split_count)





def main():
    inp = 0
    date = ""
    if len(sys.argv) == 1:
        start_date = get_yesterday()
        date = start_date
    elif len(sys.argv) == 2:
        inp = sys.argv[1]
        start_date = format_date(sys.argv[1])
        date = start_date

    check_click_info(date)


if __name__ == '__main__':
    main()

